var mainCustomerId=sessionStorage.getItem("mainCustomerId");
console.log(mainCustomerId);
function searchnotice() {
    $.ajax({
        url:root.member.findCustomerBulletin,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum":1,
            "pageSize":20,
            "status":$("#picStatus").val(),
            "title":$("#title").val(),
            "mainCustomerId":mainCustomerId
        }),
        success: function (data) {
            couponlist(data.data, 0);
            $('.M-box3').pagination({
                pageCount: data.data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        url: root.member.findCustomerBulletin,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageNum":api.getCurrent(),
                            "pageSize":20,
                            "status":$("#picStatus").val(),
                            "title":$("#title").val(),
                            "mainCustomerId":mainCustomerId

                        }),
                        success: function (result) {
                            couponlist(result.data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}

var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function couponlist(data, index){

    // var j = 0;
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组


    if(data !=null && data != ''){
        var datashu =data.startRow+1;


    $.each(data.data, function (i, field) {
        var datashus = datashu++
        var rowData = new Array();
        rowData.push(field.title);
        rowData.push(field.startTime);
        rowData.push(field.status);
        rowData.push(field.displaySequence);
        rowData.push(datashus);
        rowData.push(field.id);
        rowData.push(field.endTime);
        dataSource.push(rowData);

    });
    }
    initDataTable(dataSource);

    function initDataTable(dataSource) {

        var dttable = $('#carouselMap').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#carouselMap").DataTable({
            // "order": false,
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[6]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },

                {
                    "render": function (data, type, row, meta) {
                        var val=""
                        if (row[2] == 0) {
                            val = '<span class="dropDown"> <a class="dropDown_A" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">生效<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li onclick="statuschange(\'' + row[5] + '\',1)"><a href="#">失效</a></li></ul></span>';
                            return val;
                        } else if (row[2] == 1) {
                            val = '<span class="dropDown"> <a class="dropDown_A " data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">失效<i class="Hui-iconfont">&#xe6df;</i></a><ul class="dropDown-menu menu radius box-shadow"><li onclick="statuschange(\'' + row[5] + '\',0)"><a href="#">生效</a></li></ul></span>';
                            return val;
                        }
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 5
                },

                {
                    "render": function (data, type, row, meta) {
                        var val='';

                            val = ' <a class=""><span class="list"  onclick="noticeEdit(\'编辑\',\'noticeEdit.html\',\''+row[5]+'\')">编辑</span></a>&nbsp;&nbsp;<a class="" style="margin-right: 20px"><span class="list"  onclick="deletecarouselMap(\''+row[5]+'\')"">删除</span></a>';


                        return val;
                    },
                    "targets":6
                }


            ]
        });
//权限控制函数
        power()
    };
}
//跳转
function noticeEdit(title,url,ids) {
    sessionStorage.setItem("noticeids",ids)
    sessionStorage.setItem("noticeEdittitle",title)
    sessionStorage.setItem("noticeEditCustomerIds",mainCustomerId)
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//状态更改
function statuschange(val,stars) {
    $.ajax({
        url:root.member.updateCustomerBulletin,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "id":val,
            "mainCustomerId":mainCustomerId,
            "status":stars,
        }),
        success: function (data) {
            if(data.errorCode==0){
                $.Huimodalalert("修改成功",2000);
                searchnotice()
            }else {
                $.Huimodalalert(data.msg,2000);
            }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
//删除
function deletecarouselMap(val) {
    $.ajax({
        url:root.member.updateCustomerBulletin,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "id":val,
            "mainCustomerId":mainCustomerId,
            "isDelete":1,
        }),
        success: function (data) {
            if(data.errorCode==0){
                $.Huimodalalert("删除成功",2000);
                searchnotice()
            }else {
                $.Huimodalalert(data.msg,2000);
            }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}